# coding: utf-8

import pandas as pd
import os
import pytz
from datetime import datetime
import pymongo
import json
from time import time

"""
把1分钟线行情数据导入mongodb
"""

t0 = time()
path = 'D:\\wks_python\\allfutures_data\\stock201602mincsv\\'

filelist = os.listdir(path)
column_names = {0: 'date', 1: 'barTime', 2: 'open', 3: 'high', 4: 'low', 5: 'close', 6: 'volume', 7: 'amount'}
tz = pytz.timezone(pytz.country_timezones('cn')[0])
client = pymongo.MongoClient('121.40.212.219', 27017)  # '121.40.212.219', 27017
db = client.Minute_data

fi = open('log.txt', 'a')

for filename in filelist:
    symbol = filename.split('.')[0]
    filepath = path + os.sep + filename
    df = pd.read_csv(filepath, header=None)
    df.rename(columns=column_names, inplace=True)
    df['symbol'] = symbol
    print symbol
    df['dateTime'] = df['date'] + ' ' + df['barTime']
    df['dateTime'] = df['dateTime'].apply(lambda x: tz.localize(datetime.strptime(x, '%Y/%m/%d %H:%M')))
    # df['priceChange']=df['close']-df['close'].shift(1)
    # df['pChange']=(df['close']/df['close'].shift(1)-1)*100
    # db.one_min_data.insert_many(df.to_dict(outtype='records'))
    db.one_min_data.insert_many(df.to_dict(orient='records'))
    fi.write(filename + "\n")

fi.close()

print('finished in %.2fs' % (time() - t0))
